package cn.bouncyslime.graduationProject.nba.crawler.scheduler;

import cn.bouncyslime.graduationProject.nba.crawler.pipeline.CompetitionPipeline;
import cn.bouncyslime.graduationProject.nba.crawler.spider.NewCompetitionPageProcessor;
import cn.bouncyslime.graduationProject.nba.dao.CompetitionDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;

/**
 * @author 张一卓
 * @date 2020/5/8 17:16
 **/
@Component
public class TimingCrawlScheduler {

    @Autowired
    CompetitionDao competitionDao;

    @Autowired
    CompetitionPipeline competitionPipeline;

    @Scheduled(cron = "${schedule.cron}")
    public void timingCrawlCompetition() {
        Integer newestCompetitionId = competitionDao.getNewestCompetitionId();
        Spider.create(new NewCompetitionPageProcessor())
                .addUrl("http://www.stat-nba.com/game/" + (newestCompetitionId + 1) + ".html")
                .addPipeline(competitionPipeline)
                .thread(5).run();
    }
}
